/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.service.impl;

import com.crm.service.ISloggerService;

import com.crm.model.User;
import com.crm.model.SysLog;
import com.crm.dao.ISloggerDao;

import java.util.List;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class SloggerService implements ISloggerService {

    private static final Logger logger = Logger.getLogger(SloggerService.class);
    private ISloggerDao sloggerDao;

    public void setSloggerDao(ISloggerDao sloggerDao) {
        this.sloggerDao = sloggerDao;
    }

    /**
     * {@inheritDoc}
     */
    public void create(SysLog slog) {
        sloggerDao.saveOrUpdate(slog);
    }

    /**
     * {@inheritDoc}
     */
    public void create(User user, String clazz, String action, String info) {
        String operator = user.getTruename();
        if ("".equals(operator) || null == operator) {
            operator = user.getUsername();
        }
        SysLog slog = new SysLog(user.getId(), action, clazz, info);
        sloggerDao.saveOrUpdate(slog);
        logger.info(operator + ": " + info);
    }

    /**
     * {@inheritDoc}
     */
    public List read() {
        return sloggerDao.getLoggers();
    }

    /**
     * {@inheritDoc}
     */
    public List readByAction(String action) {
        return sloggerDao.getLoggersByAction(action);
    }

    /**
     * {@inheritDoc}
     */
    public List readByDate(Date date) {
        return sloggerDao.getLoggersByDate(date);
    }

    /**
     * {@inheritDoc}
     */
    public void delete(SysLog log) {
        sloggerDao.delete(log);
    }

    /**
     * {@inheritDoc}
     */
    public List getOperateRecord(String target, Date fromDate, Date toDate) {
        return sloggerDao.getStatistic(target, fromDate, toDate);
    }
}
